package com.iiapk.gridnew;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class LogoActivity extends Activity implements ViewFactory{
	
	ImageSwitcher imageSwitcher;
	Animation fadeAnimation;
	Animation tranSnimation;
	TimerTask timerTask;
	Timer timer;
	int index=-1;
	protected Handler iHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				int resid = images[Integer.parseInt(msg.obj.toString())];
				imageSwitcher.setBackgroundResource(resid);
				imageSwitcher.startAnimation(fadeAnimation);
				break;
			default:
				break;
			}
		}
	};
	private int[] images={R.drawable.tt1,R.drawable.tt2,
		 R.drawable.tt3,R.drawable.tt4,R.drawable.tt5};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logo);
        tranSnimation = AnimationUtils.loadAnimation(this, R.anim.push_left_out);
        tranSnimation.setAnimationListener(new AnimationListener(){
			@Override
			public void onAnimationEnd(Animation animation) {
				Intent intent = new Intent(LogoActivity.this,GridPagerActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_left_in,R.anim.hold);
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationStart(Animation animation) {
			}
        });
        findViewById(R.id.logo_go).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				v.startAnimation(tranSnimation);
				Intent intent = new Intent(LogoActivity.this,GridPagerActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_left_in,R.anim.hold);
			}
        });
        imageSwitcher = (ImageSwitcher)findViewById(R.id.logo_switcher);
        fadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade);
        imageSwitcher.setFactory(this);
        startTimer();
    }
    
    protected void startTimer() {
		if (timerTask == null) {
			timerTask = new TimerTask() {
				@Override
				public void run() {
					index++;
					if(images.length==index)
						index=0;
 					Message msg = new Message();  
	                msg.what = 1;  
	                msg.obj = index; 
	                iHandler.sendMessage(msg);  
				}
			};
			timer = new Timer();
			timer.schedule(timerTask, 0, 1000 * 7);
		}
	}

	@Override
	public View makeView() {
		return new ImageView(this); 
	}
} 
